#include <stdlib.h>
#include <stdio.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <stdint.h>

#include "header.h"

scm reg[256];
void *heap;
void *stack;

void *bytecode;
unsigned int place;
int code_length;

int execute(void);

int main(int argc, char **argv) {
  char *filename;
  struct stat st;
  int fd;
  
  if(argc != 2) {
    fprintf(stderr, "Usage: ./bytecode <filename>.byte\n");
    return -1;
  }

  filename = argv[1];
  fd = open(filename, O_RDONLY);
  if(fd == -1) {
    fprintf(stderr, "Could not open bytecode file %s\n", filename);
    return -1;
  }
  
  if(stat(filename, &st) == -1) {
    fprintf(stderr, "Could not stat bytecode file %s\n", filename);
    return -1;
  }
  
  code_length = st.st_size;
  
  bytecode = mmap(NULL, CONST_MMAP_BYTECODE_SIZE, PROT_READ, MAP_SHARED, fd, 0);
  if(bytecode == MAP_FAILED) {
    fprintf(stderr, "Memory mapping failed\n");
    return -1;
  }

  place = 0;
  
  return execute();
}
